import utils from "../utils";
import {
  login,
  getUserInfo,
  refreshToken,
  getOtherUserInfo,
} from "../Api/user";
import {
	updateAvatar,
	updateNickname,
	updateSex,
	updatePwd,
	getOtherInfo,
} from '@/Api/account.js'
const state = {
  Authorization: utils.getToken() || "",
  currentUser: utils.getUser() || "", //用户保存当前用户的账号
  userInfo: {}, //用户的个人信息
  otherInfo: {}, //他人的个人信息
};
const mutations = {
  SET_AUTHORIZATION(state, data) {
    state.Authorization = data; //token存储到state中
    utils.setToken(data); //将token存入本地
  },
  SET_CURRENTUSER(state, data) {
    state.currentUser = data; //用户信息存储到state中
    utils.setUser(data); //将用户信息存入本地
  },
  SET_USERINFO(state, data) {
    state.userInfo = data; //用户信息存储到state中
  },
  SET_OTHERINFO(state, data) {
    state.otherInfo = data; //用户信息存储到state中
  },
  SET_AVATAR(state, data) {
		state.userInfo.avatar = data
		console.log(state.userInfo)
	},
	SET_NICKNAME(state, data) {
		console.log(data)
		state.userInfo.nickName = data
	},
	SET_SEX(state, data) {
		state.userInfo.sex = data
	},
};
const actions = {
  async updatePwd(contex, Pwd) {
		let result = await updatePwd(Pwd)
		console.log(result)
		if (result.data.status == 200) {
			return 'success'
		} else
			return result.data.msg
	},
	updateSex(context, sex) {
		updateSex(sex).then(res => {
			if (res.data.status == 200)
				context.commit('SET_SEX', sex)
		})
	},
	updateNickname(context, nickname) {
		console.log(nickname)
		updateNickname(nickname).then(res => {
			if (res.data.status == 200)
				context.commit('SET_NICKNAME', nickname)
		})
	},

	updateAvatar(context, avatar) {
		console.log(111, avatar)
		updateAvatar(avatar).then(res => {
			if (res.data.status == 200)
				context.commit('SET_AVATAR', avatar)
		})
	},
  //根据id查询他人的个人信息
  getOtherInfo({ commit }, id) {
    getOtherUserInfo(id).then((res) => {
      if (res.data.status == 200) commit("SET_OTHERINFO", res.data.data);
    });
  },

  //重新生成token
  async refreshToken({ commit }) {
    let result = await refreshToken();
    if (result.data.status == 200) {
      // 刷新token成功后，将新的token提交至mutations
      commit("SET_AUTHORIZATION", result.data.token);
      let res = await this.dispatch("getUserInfo"); //刷新token后，重新获取用户信息
      if (res == "success") return "success";
    } else {
      return "error";
    }
  },
  //获取用户信息
  async getUserInfo({ commit }) {
    const result = await getUserInfo();
    if (result.data.status == 200) {
      commit("SET_USERINFO", result.data.data);
      return "success";
    }
  },
  //登录
  async login(context, data) {
    const result = await login(data.email, data.password);
    if (result.data.status == 200) {
      context.dispatch("getUserInfo"); //登录成功后，重新获取用户信息
      context.dispatch("getClassifyList");
      context.dispatch("getCart");
      context.commit("SET_AUTHORIZATION", result.data.token); //将登陆成功的结果传递给mutations
      return "success"; //返回success
    } else {
      return result.data.msg;
    }
  },
};
const getters = {
  userInfo() {
    return state.userInfo || {};
  },
  otherInfo() {
    return state.otherInfo || {};
  },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
